﻿
namespace KlearTweet.Web
{
    public partial class Default : System.Web.UI.Page
    {
        private static string _proxy;

        protected string InitParams
        {
            get { return SilverlightInitParams.Attributes["value"]; }
            set { SilverlightInitParams.Attributes["value"] = value; }
        }


        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached) // Page_Load without try/catch
                OnPageLoad();
            else
                try
                {
                    OnPageLoad();
                }
                catch (System.Exception ex)
                {
                    var error = EventLogger.Error("Default.OnPageLoad()", ex);
                    throw new System.Exception(error); // TODO: Add an ErrorPage.aspx!
                }
        }


        protected void OnPageLoad()
        {
            EventLogger.Info("Default.OnPageLoad()");

            if (_proxy == null) // TODO: Find out if it is consuming to call RoleManager.GetConfigurationSetting() and remove this field if it isn't
                _proxy = Microsoft.ServiceHosting.ServiceRuntime.RoleManager.GetConfigurationSetting("TwitterClientProxy");
            InitParams = "TwitterClient.Proxy=" + _proxy;

            var newAccount = Storage.AccountsManager.TryGetAccount(Request);
            if (newAccount != null)
            {
                EventLogger.Info("NewAccount: " + newAccount.UserName);

                InitParams += string.Format(",Account.UserName={0},Account.AuthToken={1},Account.ApplicationConsumerKey={2}",
                    newAccount.UserName, newAccount.AuthToken, newAccount.ApplicationConsumerKey);

                // TODO: Find an elequant solution (or ignore?) to remove oauth_token
            }
        }
    }
}
